package com.lin.genericstest;
/**
 * 泛型方法
 * @author Zhang
 *
 */

public class GenericsMethodDemo {
	
	/*泛型方法中可以定义泛型参数，此时，参数的类型就是传入数据的类型。*/
	public static void method1(){
		MethodDemo1 demo = new MethodDemo1();
		String str = demo.fun("hello");
		Integer count = demo.fun(100);
		Boolean bl = demo.fun(false);
		System.out.println("String:"+str+" Integer:"+count+" Boolean:"+bl);
	}
	/*通过泛型方法，返回泛型类的实例*/
	public static <T> MethodDemo2<T> method2(T param){
		MethodDemo2<T> temp = new MethodDemo2<T>();
		temp.setVar(param);
		return temp;
	}
	/*使用泛型，统一传递参数的类型*/
	public static <T> void method3(MethodDemo2<T> demo1,MethodDemo2<T> demo2){
		System.out.println("demo1："+demo1.getVar()+"  demo2:"+demo2.getVar());
	}
	public static void main(String[] args) {
		method1();
		
		MethodDemo2<String> md1 = method2("hello");
		System.out.println(md1.getVar());
		
		MethodDemo2<String> md2 = method2("world");;
		method3(md1, md2);
	}
}

class MethodDemo1{
	public <T> T fun(T t){
		return t;
	}
}

class MethodDemo2<T>{
	private T var;

	public T getVar() {
		return var;
	}

	public void setVar(T var) {
		this.var = var;
	}
	
	public String toString() {
		return this.var.toString();
	}
}

